package com.ofhb.bean;

import java.util.Date;
import java.util.logging.Logger;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.ofhb.entities.HallEntity;
import com.ofhb.entities.UserEntity;

/**
 * Session Bean implementation class DataWriterBean
 */
@Stateless(name = "DataWriterBean", mappedName = "ejb/DataWriterBean")
@LocalBean
public class DataWriterBean implements DataWriterBeanRemote {
    
    Logger logger = Logger.getLogger(DataWriterBean.class.getName());

    @PersistenceContext(name = "my_persistence_ctx")
    EntityManager em;

    /**
     * Default constructor.
     */
    public DataWriterBean() {
	// TODO Auto-generated constructor stub
    }

    @Override
    public UserEntity save(UserEntity userEntity) {
		try {
			userEntity.setCreatedDate(new Date());
			em.persist(userEntity);
			em.flush();
			logger.info(userEntity.getClass().getCanonicalName()
					+ " : Saved Successfully => ID : " + userEntity.getId());
			return userEntity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }

	@Override
	public HallEntity save(HallEntity hallEntity) {
		try {
			hallEntity.setActive(true);
			em.persist(hallEntity);
			em.flush();
			logger.info(hallEntity.getClass().getCanonicalName()
					+ " : Saved Successfully => ID : " + hallEntity.getId());
			return hallEntity;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
    }

}
